 var app = angular.module('starter', ['ionic', 'starter.controllers', 'publish.cibtent.controllers'])

 //tabs 的隐藏控制
 app.directive('hideTabs', function($rootScope) {

 	return {
 		restrict: 'A',
 		link: function(scope, element, attributes) {

 			scope.$on('$ionicView.beforeEnter', function() {

 				scope.$watch(attributes.hideTabs, function(value) {
 					$rootScope.hideTabs = 'tabs-item-hide';
 				});
 			});

 			scope.$on('$ionicView.beforeLeave', function() {
 				scope.$watch(attributes.hideTabs, function(value) {
 					$rootScope.hideTabs = 'tabs-item-hide';
 				});
 				scope.$watch('$destroy', function() {
 					$rootScope.hideTabs = false;
 				})

 			});
 		}
 	};
 })
 app.directive('fabMenu', function($timeout, $ionicGesture) {

 		var options = {
 				baseAngle: 270,
 				rotationAngle: 30,
 				distance: 112,
 				animateInOut: 'all', // can be slide, rotate, all
 			},
 			buttons = [],
 			buttonContainers = [],
 			buttonsContainer = null,
 			lastDragTime = 0,
 			currentX = 0,
 			currentY = 0,
 			previousSpeed = 15,

 			init = function() {

 				buttons = document.getElementsByClassName('fab-menu-button-item');
 				buttonContainers = document.querySelectorAll('.fab-menu-items > li');
 				buttonsContainer = document.getElementsByClassName('fab-menu-items');

 				for(var i = 0; i < buttonContainers.length; i++) {

 					var button = buttonContainers.item(i);
 					var angle = (options.baseAngle + (options.rotationAngle * i));
 					button.style.transform = "rotate(" + options.baseAngle + "deg) translate(0px) rotate(-" + options.baseAngle + "deg) scale(0)";
 					button.style.WebkitTransform = "rotate(" + options.baseAngle + "deg) translate(0px) rotate(-" + options.baseAngle + "deg) scale(0)";
 					button.setAttribute('angle', '' + angle);
 				}
 			},

 			animateButtonsIn = function() {
 				for(var i = 0; i < buttonContainers.length; i++) {

 					var button = buttonContainers.item(i);
 					var angle = button.getAttribute('angle');
 					button.style.transform = "rotate(" + angle + "deg) translate(" + options.distance + "px) rotate(-" + angle + "deg) scale(1)";
 					button.style.WebkitTransform = "rotate(" + angle + "deg) translate(" + options.distance + "px) rotate(-" + angle + "deg) scale(1)";
 				}
 			},
 			animateButtonsOut = function() {
 				for(var i = 0; i < buttonContainers.length; i++) {

 					var button = buttonContainers.item(i);
 					var angle = (options.baseAngle + (options.rotationAngle * i));
 					button.setAttribute('angle', '' + angle);
 					button.style.transform = "rotate(" + options.baseAngle + "deg) translate(0px) rotate(-" + options.baseAngle + "deg) scale(0)";
 					button.style.WebkitTransform = "rotate(" + options.baseAngle + "deg) translate(0px) rotate(-" + options.baseAngle + "deg) scale(0)";
 				}
 			},

 			rotateButtons = function(direction, speed) {

 				// still looking for a better solution to handle the rotation speed
 				// the direction will be used to define the angle calculation

 				// max speed value is 25 // can change this :)
 				// used previousSpeed to reduce the speed diff on each tick
 				speed = (speed > 15) ? 15 : speed;
 				speed = (speed + previousSpeed) / 2;
 				previousSpeed = speed;

 				var moveAngle = (direction * speed);

 				// if first item is on top right or last item on bottom left, move no more
 				if((parseInt(buttonContainers.item(0).getAttribute('angle')) + moveAngle >= 285 && direction > 0) ||
 					(parseInt(buttonContainers.item(buttonContainers.length - 1).getAttribute('angle')) + moveAngle <= 345 && direction < 0)
 				) {
 					return;
 				}

 				for(var i = 0; i < buttonContainers.length; i++) {

 					var button = buttonContainers.item(i),
 						angle = parseInt(button.getAttribute('angle'));

 					angle = angle + moveAngle;

 					button.setAttribute('angle', '' + angle);

 					button.style.transform = "rotate(" + angle + "deg) translate(" + options.distance + "px) rotate(-" + angle + "deg) scale(1)";
 					button.style.WebkitTransform = "rotate(" + angle + "deg) translate(" + options.distance + "px) rotate(-" + angle + "deg) scale(1)";
 				}
 			},

 			endRotateButtons = function() {

 				for(var i = 0; i < buttonContainers.length; i++) {

 					var button = buttonContainers.item(i),
 						angle = parseInt(button.getAttribute('angle')),
 						diff = angle % options.rotationAngle;
 					// Round the angle to realign the elements after rotation ends
 					angle = diff > options.rotationAngle / 2 ? angle + options.rotationAngle - diff : angle - diff;

 					button.setAttribute('angle', '' + angle);

 					button.style.transform = "rotate(" + angle + "deg) translate(" + options.distance + "px) rotate(-" + angle + "deg) scale(1)";
 					button.style.WebkitTransform = "rotate(" + angle + "deg) translate(" + options.distance + "px) rotate(-" + angle + "deg) scale(1)";
 				}
 			};

 		return {
 			templateUrl: "templates/fab-menu.html",
 			link: function(scope) {
 				console.info("fab-menu :: link");

 				init();

 				scope.fabMenu = {
 					active: false
 				};

 				var menuItems = angular.element(buttonsContainer);

 				$ionicGesture.on('touch', function(event) {

 					console.log('drag starts', event);
 					lastDragTime = 0;
 					currentX = event.gesture.deltaX;
 					currentY = event.gesture.deltaY;
 					previousSpeed = 0;

 				}, menuItems)

 				$ionicGesture.on('release', function(event) {
 					console.log('drag ends');
 					endRotateButtons();
 				}, menuItems);

 				$ionicGesture.on('drag', function(event) {

 					if(event.gesture.timeStamp - lastDragTime > 100) {

 						var direction = 1,
 							deltaX = event.gesture.deltaX - currentX,
 							deltaY = event.gesture.deltaY - currentY,
 							delta = Math.sqrt((deltaX * deltaX) + (deltaY * deltaY));

 						if((deltaX <= 0 && deltaY <= 0) || (deltaX <= 0 && Math.abs(deltaX) > Math.abs(deltaY))) {
 							direction = -1;
 						} else if((deltaX >= 0 && deltaY >= 0) || (deltaY <= 0 && Math.abs(deltaX) < Math.abs(deltaY))) {
 							direction = 1;
 						}

 						rotateButtons(direction, delta);

 						lastDragTime = event.gesture.timeStamp;
 						currentX = event.gesture.deltaX;
 						currentY = event.gesture.deltaY;
 					}
 				}, menuItems);

 				scope.fabMenuToggle = function() {

 					if(scope.fabMenu.active) { // Close Menu
 						animateButtonsOut();
 						document.getElementById('mude1').style.WebkitTransform = "rotate(0deg)";
 						document.getElementById('mude1').style.transform = "rotate(0deg) ";

 					} else { // Open Menu
 						animateButtonsIn();
 						document.getElementById('mude1').style.WebkitTransform = "rotate(-45deg)";
 						document.getElementById('mude1').style.transform = "rotate(-45deg) ";
 					}
 					scope.fabMenu.active = !scope.fabMenu.active;
 				}

 			}
 		}
 	})
 	.run(function($ionicPlatform) {

 		$ionicPlatform.ready(function() {
 			if(window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
 				cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
 			setTimeout(function () {  
               navigator.splashscreen.hide();  
           }, 1000);  
 			}
 			if(window.StatusBar) {
 				StatusBar.styleLightContent();
 			}
 		});
 	})

 	.config(function($stateProvider, $urlRouterProvider, $ionicConfigProvider) {
 		$ionicConfigProvider.backButton.text("");
 		$ionicConfigProvider.backButton.previousTitleText(false);

 		$stateProvider
 			// 首页 启动页
 			.state('index', {
 				url: '/index',
 				templateUrl: "templates/index.html",
 				controller: 'indexCtrl'
 			})
 			//登录
 			.state('login', {

 				url: '/login',
 				templateUrl: 'templates/login.html',
 				controller: 'LoginCtrl'
 			})
 			//注册 1-3
 			.state('register', {
 				url: '/register',
 				templateUrl: 'templates/register.html',
 				controller: 'RegisterCtrl'
 			})
 			.state('register-1', {
 				url: '/register-1',
 				templateUrl: 'templates/register-1.html',
 				controller: 'Register_1Ctrl'
 			})
 			.state('user', {
 				url: '/user/:userId',
 				templateUrl: 'templates/user.html',
 				controller: 'userCtrl'
 			})
 			.state('register-2', {
 				url: '/register-2',
 				templateUrl: 'templates/register-2.html',
 				controller: 'Register_2Ctrl'

 			})
 			//忘记密码
 			.state('forget-pwd', {
 				url: '/forget',
 				templateUrl: 'templates/forget-pwd.html',
 				controller: 'forgetCtrl'

 			})
 			.state('tab', {
 				url: "/tab",
 				abstract: true,
 				templateUrl: "templates/tabs.html",
 				controller: 'tabsCtrl'
 			})
 			//精选
 			.state('tab.Selected', {
 				url: '/Selected',
 				views: {
 					'tab-Selected': {
 						templateUrl: 'templates/tab-Selected.html',
 						controller: 'SelectedCtrl'
 					}
 				}
 			})

 			//发现
 			.state('tab.faxian', {
 				url: '/faxian',
 				views: {
 					'tab-faxian': {
 						templateUrl: 'templates/tab-faxian.html'
 					}
 				}
 			})
 			//发现内容
 			.state('tab.faxian-activity', {
 				url: '/faxian/:activityId',
 				views: {
 					'tab-faxian': {
 						templateUrl: 'templates/faxian-activity.html',
 						controller: 'faxian-activityCtrl'
 					}
 				}
 			})
 			//活动内容
 			.state('huodong', {
 				url: '/huodong/:huodongId',
 				templateUrl: 'templates/huodong.html',
 				controller: 'huodong-conCtrl'

 			})
 			//活动报名内容
 			.state('huodong-bm', {
 				url: '/huodong-bm/:huodongId',
 				templateUrl: 'templates/bm.html',
 				controller: 'bmCtrl'

 			})
 			//活动内容-amp
 			.state('huodong-map', {

 				url: '/huodong/:huodongId/:map',
 				templateUrl: 'templates/map-juli.html',
 				controller: 'huodong_mapCtrl'

 			})
 			//玩吧
 			.state('tab.move', {
 				url: '/move',
 				views: {
 					'tab-wanba': {
 						templateUrl: 'templates/tab-move.html',
 						controller: 'MoveCtrl'
 					}
 				}
 			})
 			//发布
 			.state('tab.publish', {
 				cache: false,
 				url: '/publish',
 				views: {
 					'tab-publish': {
 						templateUrl: 'templates/tab-publish.html',
 						controller: 'publishCtrl'
 					}
 				}
 			})
 			//发布内容
 			.state('publish-content', {

 				url: '/publish/:contentId',
 				templateUrl: 'templates/publish-content.html',
 				controller: 'publish-contentCtrl'

 			})
 			//发布内容map
 			.state('publish-content-map', {
 				url: '/publish/content/map',
 				templateUrl: 'templates/map.html',
 				controller: 'mapCtrl'

 			})
 			//我
 			.state('tab.me', {
 				url: '/me',
 				views: {
 					'tab-me': {
 						templateUrl: 'templates/tab-me.html',
 						controller: 'MeCtrl'
 					}
 				}
 			})
 			//我的收藏
 			.state('tab.me-love', {
 				url: '/me/love',
 				views: {
 					'tab-me': {
 						templateUrl: 'templates/me/love-activity.html',
 						controller: 'love-activityCtrl'
 					}
 				}
 			})
 			//我的收藏
 			.state('tab.me-bmpz', {
 				url: '/me/bmpz',
 				views: {
 					'tab-me': {
 						templateUrl: 'templates/me/me-bmpz.html',
 						controller: 'me-bmpzCtrl'
 					}
 				}
 			})
 			//我的活动
 			.state('tab.me-activity', {
 				url: '/me/activity',
 				views: {
 					'tab-me': {
 						templateUrl: 'templates/me/me-activity.html',
 						controller: 'me-activityCtrl'
 					}
 				}
 			})
 			.state('tab.me-dynamic', {
 				url: '/me/dynamic',
 				views: {
 					'tab-me': {
 						templateUrl: 'templates/me/dynamic.html',
 						controller: 'dongtai1'
 					}
 				}
 			})
 			//我-设置
 			.state('tab.me-set', {
 				url: '/me/set',
 				views: {
 					'tab-me': {
 						templateUrl: 'templates/me/set.html',
 						controller: 'setCtrl'
 					}
 				}
 			})
 			//我-设置-个人资料
 			.state('tab.me-set-data', {
 				url: '/me/set/data',
 				views: {
 					'tab-me': {
 						templateUrl: 'templates/me/set-data.html',
 						controller: 'set_dataCtrl'
 					}
 				}
 			})
 			//我-设置-消息设置
 			.state('tab.me-set-news', {
 				url: '/me/set/news',
 				views: {
 					'tab-me': {
 						templateUrl: 'templates/me/set-news.html'
 					}
 				}
 			})
 			//我-设置-意见反馈
 			.state('tab.me-set-feedback', {
 				url: '/me/set/feedback',
 				views: {
 					'tab-me': {
 						templateUrl: 'templates/me/set-feedback.html',
 						controller: 'set_feedbackCtrl'
 					}
 				}
 			})
 			//我-设置-关于稚互动动
 			.state('tab.me-set-about', {
 				url: '/me/set/about',
 				views: {
 					'tab-me': {
 						templateUrl: 'templates/me/set-about.html'
 					}
 				}
 			});

 		// if none of the above states are matched, use this as the fallback
 		$urlRouterProvider.otherwise('index');
 		$ionicConfigProvider.platform.ios.tabs.style('standard');
 		$ionicConfigProvider.platform.ios.tabs.position('bottom');
 		$ionicConfigProvider.platform.android.tabs.style('standard');
 		$ionicConfigProvider.platform.android.tabs.position('bottom');

 		$ionicConfigProvider.platform.ios.navBar.alignTitle('center');
 		$ionicConfigProvider.platform.android.navBar.alignTitle('center');

 		$ionicConfigProvider.platform.ios.backButton.previousTitleText('').icon('ion-ios-arrow-thin-left');
 		$ionicConfigProvider.platform.android.backButton.previousTitleText('').icon('ion-android-arrow-back');

 		$ionicConfigProvider.platform.ios.views.transition('ios');
 		$ionicConfigProvider.platform.android.views.transition('android');
 		//$ionicConfigProvider.scrolling.jsScrolling(false);
 	});